package KatarinaBot;

import java.util.prefs.*;
import java.util.Scanner;

public class KatarinaBotMain {
	
	/******************
	 * 
	 * Global variables
	 *
	 ******************/
	private Preferences prefs = Preferences.userNodeForPackage (getClass ());
	
	public static String ircAddress = "irc.irchighway.net"; // IRC Address
	public static String channelName = "#MyBubbleteaTime"; // Name of channel
	
	/*********************
	 * 
	 *  Standard functions
	 *  
	 ********************/
	// Default constructor launch
	public static void main(String[] args) throws Exception{
		
		// Creates a new instance of the app and launches the menu
		KatarinaBotMain launch = new KatarinaBotMain();
		launch.menu();
		
	}
	
	// Main menu
    public void menu() throws Exception {
        
    	//Declarations
    	Scanner scanner = new Scanner (System.in); // Initialise input scanner
    	
    	//Variables
    	int selectedValue = 0;
    	boolean keepAlive = true;
    	
    	// Set values to global parameters
    	ircAddress = prefs.get("keyIrcAddress", "irc.irchighway.net");
    	channelName = prefs.get("keyChannelName", "#MyBubbleteaTime");
    	
    	// Launches database first run check
    	Database.main();    	
    	// Console selection screen
    	while (keepAlive == true) {
    			System.out.println("===========================================");
    		    System.out.println("| KatarinaBot v0.3 Pre-Alpha              |");
    			System.out.println("===========================================");
    		    System.out.println("| Main Menu:                              |");
    		    System.out.println("|   1. Connect to IRC                     |");
    		    System.out.println("|   2. View Connection Settings           |");
    		    System.out.println("|   3. Edit Connection Settings           |");
    		    System.out.println("|   4. Database Settings                  |");
    		    System.out.println("|   5. About                              |");
    		    System.out.println("| 0. Exit                                 |");
    			System.out.println("===========================================");
	    		System.out.print("Select Option: ");
	    		selectedValue = scanner.nextInt();
	    		
	    		// Determine user input option
    		    switch (selectedValue) {

		    		case 0:
		    			System.out.println("===========================================");
		    			System.out.println("| Bot terminated. Goodbye. =(             |");
		    			System.out.println("===========================================");
		    			keepAlive = false;
		    			break;    	//Declarations
    		    	case 1:
    		    		connectBot();
    		    		break;
    		    	case 2:
    		    		viewConnectionSettings();
    		    		break;
    		    	case 3:
    		    		editConnectionSettings();
    		    		break;
    		    	case 4:
    		    		databaseSettings();
    		    		break;
    		    	case 5:
    		    		aboutKatarinaBot();
    		    		break;
    		    		
    		    }
    	}        
    }
    
    
    // Variables to connect the bot to the specified server
    public static void connectBot() throws Exception{
    	
    	//Declarations
    	KatarinaBot bot = new KatarinaBot(); // Start the bot
    	
    	// Enable debugging output.
    	bot.setVerbose(true);
    	
    	// Connect to the IRC server
    	bot.connect(ircAddress);
    	
    	// Join the specified channel
    	bot.joinChannel(channelName);
    	
    }
    
    public static void viewConnectionSettings() {
    	
    	// Displays connection settings
		System.out.println("===========================================");
    	System.out.println("| View Connection Settings:               |");
		System.out.println("===========================================");
		System.out.println("| Here you can view your current settings.|");
		System.out.println("| To change your settings, go back to the |");
		System.out.println("| main menu.                              |");
		System.out.println("===========================================");
		System.out.println("IRC Server Address: " + ircAddress);
		System.out.println("Channel: " + channelName);
		pauseProg(); // Wait for user input
		
    }
    
    // Edit connection settings of bot
    public void editConnectionSettings() {
    	
    	//Declarations
    	Scanner scanner = new Scanner (System.in); // Initialise input scanner
    	
    	// Ask for user input to settings
		System.out.println("===========================================");
    	System.out.println("| Edit Connection Settings:               |");
		System.out.println("===========================================");
		System.out.println("| Input the correct details!              |");
		System.out.println("| Examples:                               |");
		System.out.println("| IRC Address: irc.irchighway.net         |");
		System.out.println("| Channel: #MyBubbleteaTime               |");
		System.out.println("===========================================");
    	System.out.print("Input IRC Server Address: ");
    	ircAddress = scanner.next(); // Gets IRC Address
    	prefs.put("keyIrcAddress", ircAddress); // Saves IRC Address
    	System.out.print("Input Channel Name (#Channel): ");
    	channelName = scanner.next(); // Gets name of channel
    	prefs.put("keyChannelName", channelName); // Saves name of channel
    	System.out.println("Settings saved successfully!\n");
    	pauseProg();
    	
    }
    
    // Database Settings menu
    public static void databaseSettings() throws Exception {
    	    	
    	//Declarations
    	Scanner scanner = new Scanner (System.in); // Initialise input scanner
    	int selectedValue = 0;
    	
    	// Ask for user input to settings
		System.out.println("===========================================");
    	System.out.println("| Database Settings:                      |");
		System.out.println("===========================================");
		System.out.println("| Command: !steal                         |");
		System.out.println("|   1. Add new stolen item                |");
		System.out.println("|   2. Delete stolen item                 |");
		System.out.println("|   3. View list of items                 |");
		System.out.println("|   4. Restore to default                 |");
		System.out.println("| 0. Back to Main Menu                    |");
		System.out.println("===========================================");
		System.out.print("Select Option: ");
		selectedValue = scanner.nextInt();
    	
		// Determine user input option
	    switch (selectedValue) {

    		case 0:
    			break;
	    	case 1:
	    		addNewStolenItem(scanner);
	    		break;
	    	case 2:
	    		deleteStolenItem(scanner);
	    		break;
	    	case 3:
	    		displayStolenItems();
	    		break;
	    	case 4:
	    		restoreDefaultStolenItems(scanner);
	    		break;
	    		
	    }
	    
    }
    
    // Adds a new stolen item to the database
    public static void addNewStolenItem(Scanner scanner) {
    	
    	// Declarations
    	Database databaseClass = new Database();
    	String nameOfItem;
    	
    	System.out.print("Input name of item: ");
   	
    	try {
        	nameOfItem = scanner.next();
        	databaseClass.insertNewStolenItem(nameOfItem);
    	} catch (Exception e) {
    		System.out.println("ERROR: Invalid input.");
    	}
	
    }

    // Deletes an item from the database
    public static void deleteStolenItem(Scanner scanner) {
    	
    	// Declarations
    	Database databaseClass = new Database();
    	String nameOfItem;
    	
    	System.out.print("Input name of item: ");
    	
    	try {
    		nameOfItem = scanner.next();
    		
    		if (nameOfItem != null) {
        		databaseClass.deleteStolenItem(nameOfItem);
    		} else {
    			System.out.println("Item not deleted from database.");
    		}
    	} catch (Exception e) {
    		System.out.println("ERROR: Invalid input.");
    	}
    	
    }
    
    // Displays the list of items
    public static void displayStolenItems() throws Exception {
    	
    	Database databaseClass = new Database();
    	
    	databaseClass.displayStolenItems();
    	
    }
    
    // Restores to default database
    public static void restoreDefaultStolenItems(Scanner scanner) {
    	
    	Database databaseClass = new Database();
    	
    	try {
			databaseClass.restoreToDefaultStolenItemsDatabase();
		} catch (Exception e) {
			System.out.println("ERROR: Unable to restore database to default!");
		}
	
    }
    
    // Displays about information of the bot
    public static void aboutKatarinaBot() {
    	    	
    	// Prints about information
		System.out.println("===========================================");
    	System.out.println("| About:                                  |");
		System.out.println("===========================================");
		System.out.println("| KatarinaBot was created by MegaBubbletea|");
		System.out.println("| as a personal project for his website's |");
		System.out.println("| IRC channel. It has been extended to in |");
		System.out.println("| clude additional features to make it    |");
		System.out.println("| connect to other IRC sites and channels.|");
		System.out.println("|                                         |");
		System.out.println("| The project is under GNU GPLv3 license. |");
		System.out.println("| http://www.gnu.org/copyleft/gpl.html    |");
		System.out.println("|                                         |");
		System.out.println("| Visit my site here:                     |");
		System.out.println("| www.mybubbleteatime.com                 |");
		System.out.println("|                                         |");
		System.out.println("| (C)2012. My Bubbletea Time.             |");
		System.out.println("===========================================");
		pauseProg(); // Wait for user input
		
    }

    // Pauses the program and waits for user input
    public static void pauseProg() {
    	
    	// Pauses the program and waits for user input
		System.out.print("Press <Enter> to continue...");
		Scanner keyboard = new Scanner(System.in);
		keyboard.nextLine();
		
	}

}
